Skip to content

最初のGETリクエスト

このチュートリアルでは、あなたのaoプロセスを通じて0rbitネットワークにGETリクエストを送信する方法を学びます。

🔑 前提条件

  • システムにaosがインストールされていること。
  • 一部の$0RBT。こちらで$0RBTを入手する方法を学ぶこちら_
  • 任意のコードエディタ(VSCode、Sublime Textなど)

上記の前提条件が整ったら、

🛠️ 開発を始めましょう

プロジェクトの初期化

プロジェクトディレクトリに0rbit-Get-Request.luaという新しいファイルを作成します。

bash
touch 0rbit-Get-Request.lua

変数の初期化

lua
local json = require("json")

_0RBIT = "BaMK1dfayo75s3q1ow6AO64UDpD9SEFbeE8xYrY2fyQ"
_0RBT_POINTS = "BUhZLMwQ6yZHguLtJYA5lLUa9LQzLXMXRfaq9FVcPJc"

FEE_AMOUNT = "1000000000000" -- 1 $0RBT
BASE_URL = "https://api.diadata.org/v1/assetQuotation/Arweave/0x0000000000000000000000000000000000000000"

ReceivedData = ReceivedData or {}

リクエストの送信

以下のコードには、0rbitプロセスに1 $0RBTを送信し、BASE_URLに対してGETリクエストを行うハンドラが含まれています。

lua
Handlers.add(
    "Get-Request",
    Handlers.utils.hasMatchingTag("Action", "First-Get-Request"),
    function(msg)
        Send({
            Target = _0RBT_TOKEN,
            Action = "Transfer",
            Recipient = _0RBIT,
            Quantity = FEE_AMOUNT,
            ["X-Url"] = BASE_URL,
            ["X-Action"] = "Get-Real-Data"
        })
        print(Colors.green .. "You have sent a GET Request to the 0rbit process.")
    end
)

上記のコードの内訳:

  • Handlers.addは、aoプロセスに新しいハンドラを追加するために使用されます。
  • Get-Requestはハンドラの名前です。
  • Handlers.utils.hasMatchingTagは、受信メッセージがFirst-Get-Requestと同じタグを持っているかをチェックする関数です。
  • function(msg)は、ハンドラが呼び出されたときに実行される関数です。
  • Sendは、いくつかのタグを引数として取り、ao上にメッセージを作成する関数です。
TagDescription
TargetThe processId of the recipient. In this case, it's the $0RBT token processId.
ActionThe tag that defines the handler to be called in the recipient process. In this case it's Transfer
RecipientThe tag that accepts the processId to whom the $0RBT will be sent. In this case, it's the 0rbit processId.
QuantityThe amount of $0RBT to be sent.
["X-Url"]The forwarded-tag which contains the URL and the same will be used by the 0rbit process to fetch the data.
["X-Action"]The forwarded-tag which contains the action to be performed by the 0rbit process. In this case, it's Get-Real-Data.

データの受信

以下のコードには、0rbitプロセスからデータを受信して印刷するハンドラが含まれています。

lua
Handlers.add(
    "Receive-Data",
    Handlers.utils.hasMatchingTag("Action", "Receive-Response"),
    function(msg)
        local res = json.decode(msg.Data)
        ReceivedData = res
        print(Colors.green .. "You have received the data from the 0rbit process.")
    end
)

上記のコードの内訳:

  • Handlers.addは、aoプロセスに新しいハンドラを追加するために使用されます。
  • Receive-Dataはハンドラの名前です。
  • Handlers.utils.hasMatchingTagは、受信メッセージがReceive-Responseと同じタグを持っているかをチェックする関数です。
  • function(msg)は、ハンドラが呼び出されたときに実行される関数です。
    • json.decodeは、受信したJSONデータをデコードするために使用されます。
    • ReceivedData = resは、受信したデータをReceivedData変数に格納します。

🏃 プロセスを実行する

新しいプロセスを作成し、スクリプトを読み込む

bash
aos 0rbitGetRequest --load 0rbit-Get-Request.lua

上記のコマンドは、0rbitGetRequestという名前の新しいプロセスを作成し、0rbit-Get-Request.luaを読み込みます。

ハンドラの呼び出し

ハンドラを呼び出して、0rbitプロセスへのリクエストを作成します。

bash
Send({ Target= ao.id, Action="First-Get-Request" })

成功裏に実行されると、ターミナルに以下のメッセージが表示されます。

データの確認

ReceivedData変数に格納されたデータを確認するには、以下のコマンドを実行します:

bash
ReceivedData

成功裏に実行されると、ターミナルにJSONデータが表示されます:

json
{
   Address = "0x0000000000000000000000000000000000000000",
   Name = "Arweave",
   Blockchain = "Arweave",
   Signature = "0x2cd98c6f29a044d732ffcbc1a1b11e6f93f97f760dd1c9e47717ca04cc500afd6d83ad65270b227ddbaeba713e329e31959c814620d8ca136e685565414673d101",
   Time = "2024-08-14T14:05:59Z",
   Source = "diadata.org",
   PriceYesterday = 21.446763148012,
   Symbol = "AR",
   Price = 21.404398988798,
   VolumeYesterdayUSD = 14609998.02428
}

やった!あなたは成功裏に0rbitプロセスで最初のGETリクエストを行いました。🎉

You can find the complete code here:

https://github.com/0rbit-co/examples/blob/main/First-Get-Request.lua